package hkr.budget;

import java.io.File;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import com.example.budget.R;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.ToggleButton;
import android.widget.ViewFlipper;

public class RegisterMain extends Activity implements OnClickListener {

	public static final int SETUP_OPTION = 1;
	private BudgetDataSource mQueryDB;
	private Button mClearButton;
	private Button mSubmitButton;
	private DatePicker mDatePicker;
	private EditText mAmountEdit;
	private EditText mExpenseEdit;
	private ImageButton mRegisterButton;
	private ImageButton mPieChartButton;
	private ImageButton mBarChartButton;
	private ImageButton mSpreadSheetButton;
	private Spinner mCategorySpinner;
	private ToggleButton mCreditCardToggle;
	private ViewFlipper vf;




	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_main);
		mClearButton = (Button) findViewById(R.id.btnClear);
		mSubmitButton = (Button) findViewById(R.id.btnSubmit);
		mDatePicker = (DatePicker) findViewById(R.id.pkrDate);
		mAmountEdit = (EditText) findViewById(R.id.txtAmount);
		mExpenseEdit = (EditText) findViewById(R.id.txtExpense);
		mRegisterButton = (ImageButton) findViewById(R.id.btnRegister);
		mPieChartButton = (ImageButton) findViewById(R.id.btnPieChart);
		mBarChartButton = (ImageButton) findViewById(R.id.btnBarChart);
		mSpreadSheetButton = (ImageButton) findViewById(R.id.btnSpreadSheet);
		mCategorySpinner = (Spinner) findViewById(R.id.spnCategory);
		mCreditCardToggle = (ToggleButton) findViewById(R.id.tglCreditCard);

		setEditTextKeyListener(mExpenseEdit, mAmountEdit, false);
		setEditTextKeyListener(mAmountEdit, mSubmitButton, true);
		mClearButton.setOnClickListener(this);
		mSubmitButton.setOnClickListener(this);
		mRegisterButton.setOnClickListener(this);
		mPieChartButton.setOnClickListener(this);
		mBarChartButton.setOnClickListener(this);
		mSpreadSheetButton.setOnClickListener(this);
		mCreditCardToggle.setOnClickListener(this);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this, R.array.expense_catagories, android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mCategorySpinner.setAdapter(adapter);
		Intent setupIntent = new Intent(this, Setup.class);
		//if(!doesDatabaseExist(this, DatabaseBuilder.DATABASE_NAME)){
		//	startActivity(setupIntent);
		//}

		mQueryDB = new BudgetDataSource(this);
		mQueryDB.open();

		getChart();
        vf = (ViewFlipper) findViewById(R.id.viewFlipper1);


	}

	private void setEditTextKeyListener(EditText editText, final View nextView, final boolean hideKeyboard) {
		editText.setOnEditorActionListener(new OnEditorActionListener(){

			@Override
			public boolean onEditorAction(TextView textView, int actionId, KeyEvent event){

				if(event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)){
					InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

					if(hideKeyboard){

						inputManager.hideSoftInputFromWindow(textView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
					}
					// userValidateEntry();
					textView.clearFocus();
					nextView.requestFocus();

					return true;
				}

				return false;

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
			menu.add(0,SETUP_OPTION, 0, "Setup Options");
			return super.onCreateOptionsMenu(menu);
		}

	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);

		if(resultCode == RESULT_OK){

		}
	}

	private static boolean doesDatabaseExist(ContextWrapper context, String dbName) {
	    File dbFile=context.getDatabasePath(dbName);
	    return dbFile.exists();
	}


	@Override
	public void onClick(View v) {
		Log.d("Register", "View ID is: " + v.getId());
		switch(v.getId()){

		case R.id.btnRegister:
			vf.setDisplayedChild(0);
			Log.d("Register", "Regster button selected!");

			break;

		case R.id.btnPieChart:
			vf.setDisplayedChild(1);
			//vf.showNext();
			Log.d("Register", "Pie chart button selected!");
			Log.i("Register", "Pie chart button selected!");

			break;

		case R.id.btnBarChart:

			Log.d("Register", "Bar chart button selected!");
			vf.setDisplayedChild(2);

			break;

		case R.id.btnSpreadSheet:
			vf.setDisplayedChild(3);
			Log.d("Register", "Spread sheet button selected!");

			break;

		case R.id.btnClear:
			mCreditCardToggle.setChecked(false);
			mExpenseEdit.setText("");
			mAmountEdit.setText("");

			break;

		case R.id.btnSubmit:

			String date = mDatePicker.getDayOfMonth() + "/" + mDatePicker.getMonth() + "/" + mDatePicker.getYear();
			String paymentType = (mCreditCardToggle.isChecked()) ? "Credit Card" : "Cash";
			mQueryDB.saveExpense(date, mExpenseEdit.getText().toString(),
								mCategorySpinner.getSelectedItem().toString(),
								paymentType, Double.parseDouble(mAmountEdit.getText().toString()));

			mCreditCardToggle.setChecked(false);
			mExpenseEdit.setText("");
			mAmountEdit.setText("");
			break;


		}

	}

    private void openChart() {
		// TODO Auto-generated method stub

	}

	private void getChart(){

    	View view = Graph.getPieChart(getBaseContext());

        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.viewGraph);
        relativeLayout.addView(view);

    }



}
